<?php

namespace Acme\DemoBundle\Repository;

use Doctrine\ORM\EntityRepository;

class PhongBanRepository extends EntityRepository
{
	public function findAllByCoditions()	{
	
		$qb = $this->createQueryBuilder('p')
		->where('p.deleted is null')
		->orderBy('p.id' , 'DESC');
		return $qb->getQuery()->getResult();
	}
	public function findByCoditions($offset,$limit)	{
		
		$qb = $this->createQueryBuilder('p')
		->where('p.deleted is null')
		->orderBy('p.id' , 'DESC')
		->setFirstResult( $offset )
		->setMaxResults( $limit );
		return $qb->getQuery()->getResult();
	}
	public function countAll()	{
	
		$qb = $this->createQueryBuilder('p');
		return $qb->getQuery()->getResult();
	}
	public function savePhongBan($phongban) {
		$em = $this->getEntityManager();
		$em->persist($phongban);
		$em->flush();
	}
	
	
	public function checkMaInsert($ma)	{
	
		$qb = $this->createQueryBuilder('p')
		->where('p.deleted is null')
		->andWhere('p.ma = :ma')
		->setParameter('ma', $ma);
	
		return $qb->getQuery()->getResult();
	}
	
	public function checkMaUpdate($ma ,$macu)	{
	
		$qb = $this->createQueryBuilder('p')
		->where('p.deleted is null')
		->andWhere('p.ma = :ma')
		->andWhere('p.ma != :macu')
		->setParameter('ma', $ma)
		->setParameter('macu', $macu);
	
		return $qb->getQuery()->getResult();
	}
}